package com.eight.cloud.payment.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.eight.cloud.core.global.exception.CustomException;
import com.eight.cloud.core.global.result.ResultCode;
import com.eight.cloud.core.utils.format.Validate;
import com.eight.cloud.mybatisplus.service.BaseService;
import com.eight.cloud.payment.common.enums.PayChannelEnum;
import com.eight.cloud.payment.common.enums.TradeTypeEnum;
import com.eight.cloud.payment.entity.SubRefundOrderEntity;
import com.eight.cloud.payment.mapper.SubRefundOrderMapper;
import com.eight.cloud.payment.model.dto.SubRefundOrderDto;
import com.eight.cloud.payment.model.vo.SubRefundOrderVo;
import com.eight.cloud.payment.service.ISubRefundOrderService;
import com.eight.cloud.redisson.utils.RedisUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.eight.cloud.core.global.constant.NumConstant.*;

/**
* 退款记录表 服务实现类
*
* @ClassName: SubRefundServiceImpl
* @Author TXC
* @Date 2024-12-24 16:24
*/
@Service
public class SubRefundOrderServiceImpl extends BaseService<SubRefundOrderMapper, SubRefundOrderEntity> implements ISubRefundOrderService {
    @Resource
    private RedisUtils redisUtils;

    @Override
    public SubRefundOrderVo addOrder(SubRefundOrderDto subRefundOrderDto) {
        if (ObjectUtil.isNull(subRefundOrderDto)) {
            throw new CustomException(ResultCode.PARAM_IS_BLANK);
        }
        Validate.checkField(ObjectUtil::isEmpty, subRefundOrderDto.getOutTradeNo(), "商户号");
        Validate.checkField(ObjectUtil::isEmpty, subRefundOrderDto.getMerchantId(), "商户号");
        Validate.checkField(ObjectUtil::isEmpty, subRefundOrderDto.getAppid(), "应用id");
        Validate.checkField(ObjectUtil::isEmpty, subRefundOrderDto.getTradeType(), "支付方式");
        Validate.checkField(ObjectUtil::isEmpty, subRefundOrderDto.getPayChannel(), "支付类型");
        Validate.checkField(ObjectUtil::isEmpty, subRefundOrderDto.getRefund(), "退款金额");
        Validate.checkField(ObjectUtil::isEmpty, subRefundOrderDto.getTotal(), "订单金额");
        if (subRefundOrderDto.getRefund() == ZERO) {
            throw new CustomException(500, "退款金额不能为0");
        }
        if (subRefundOrderDto.getRefund() > subRefundOrderDto.getTotal()) {
            throw new CustomException(500, "退款金额不能大于订单金额");
        }
        SubRefundOrderEntity subRefundOrderEntity = convertToEntity(subRefundOrderDto);
        subRefundOrderEntity.setUuid(redisUtils.generateSerial(EIGHT, TWO, "refund", "order", "R", subRefundOrderDto.getAppid()));
        subRefundOrderEntity.setStatus(SubRefundOrderEntity.RefundStatusEnum.FAILED.getValue());
        // 如果子商户退款订单号为空，则用系统订单号代替
        if (StrUtil.isBlank(subRefundOrderEntity.getSubRefundNo())) {
            subRefundOrderEntity.setSubRefundNo(subRefundOrderEntity.getUuid());
        }
        baseMapper.insert(subRefundOrderEntity);
        SubRefundOrderVo subRefundOrderVo = convertToObject(subRefundOrderEntity, SubRefundOrderVo.class);
        subRefundOrderVo.setTradeType(TradeTypeEnum.getByCode(subRefundOrderEntity.getTradeType()));
        subRefundOrderVo.setPayChannel(PayChannelEnum.getByCode(subRefundOrderEntity.getPayChannel()));
        return subRefundOrderVo;
    }

    @Override
    public SubRefundOrderVo findSubRefundOrder(String merchantId, String subRefundNo, String appid) {
        SubRefundOrderEntity order = lambdaQuery().eq(SubRefundOrderEntity::getMerchantId, merchantId)
                .eq(SubRefundOrderEntity::getSubRefundNo, subRefundNo)
                .eq(SubRefundOrderEntity::getAppid, appid).one();
        SubRefundOrderVo subRefundOrderVo = convertToObject(order, SubRefundOrderVo.class);
        subRefundOrderVo.setTradeType(TradeTypeEnum.getByCode(order.getTradeType()));
        subRefundOrderVo.setPayChannel(PayChannelEnum.getByCode(order.getPayChannel()));
        return subRefundOrderVo;
    }

    @Override
    public boolean updateSubRefundOrder(String uuid, String transactionId, Integer status) {
        if (StrUtil.isNotBlank(uuid)) {
            return baseMapper.update(lambdaUpdateWrapper()
                    .set(StrUtil.isNotBlank(transactionId), SubRefundOrderEntity::getTransactionId, transactionId)
                    .set(ObjectUtil.isNotNull(status), SubRefundOrderEntity::getStatus, status)
                    .eq(SubRefundOrderEntity::getUuid, uuid)) > ZERO;
        }
        throw new CustomException(ResultCode.PARAM_IS_BLANK);
    }
}
